package com.smartwebee.android.blespp.blockly;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.blockly.base.BlockBaseActivity;
import com.smartwebee.android.blespp.blockly.js.JsInterface;
import com.smartwebee.android.blespp.databinding.ActivityBlockIndexBinding;
import com.smartwebee.android.blespp.eventbusclass.RegisterSucEvent;
import com.smartwebee.android.blespp.eventbusclass.SelectDevice;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Scheduler;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class BlockIndexActivity extends BlockBaseActivity {

    private StringBuilder sb;
    private ActivityBlockIndexBinding binding;
    private Subscription subscription;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_block_index);
        initView();
    }

    private void initView() {
        sb = new StringBuilder();
        initWebView();
        binding.webview.addJavascriptInterface(new JsInterface(this, binding.webview), "android");
        binding.webview.loadUrl("file:///android_asset/Blockly/demos/mydemo/DemoTry.html");
        subscription = Observable.timer(2, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        binding.imgPlaceholder.setVisibility(View.GONE);
                    }
                });
    }

    private void initWebView() {
        binding.webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
            }
        });
        binding.webview.setWebChromeClient(new WebChromeClient() {
        });
        binding.webview.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.webview.setScrollContainer(false);
        WebSettings webSettings = binding.webview.getSettings();
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(false);
//        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLoadWithOverviewMode(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
    }

    @Override
    protected void displayData(byte[] buf) {
        String s = BlocklyUtils.asciiToString(buf);
        Log.i("displayData", s);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SelectDevice device) {
        mDeviceName = device.getName();
        mDeviceAddress = device.getAddress();
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Override
    protected void whenDismissLoading() {
        String jsStr = "javascript:DemoApp.showLoadSuccess()";
        binding.webview.evaluateJavascript(jsStr, null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }
}
